<template>
  <div>
    <el-button
      type="primary"
      plain
      icon="el-icon-plus"
      size="mini"
      @click="handleAdd"
      v-hasPermi="['system:swmd:add']"
    >公安新增
    </el-button>

    <!-- 公安数据新增对话框 -->
    <el-dialog title="公安数据新增" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <div class="module-section">
          <div class="module-header">
            <i class="el-icon-money"></i>
            <span class="module-title">公安数据</span>
            <span class="module-remark">（所有带*字段均为必填）</span>
          </div>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="姓名" prop="xm">
                <el-input v-model="form.xm" placeholder="请输入姓名" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="xb">
                <el-select v-model="form.xb" placeholder="请选择性别" clearable style="width: 100%">
                  <el-option label="男" value="0" />
                  <el-option label="女" value="1" />
                  <el-option label="未知" value="2" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="证件号码" prop="sfzh">
                <el-input v-model="form.sfzh" placeholder="请输入证件号码" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="户籍所在地" prop="hjszd">
                <el-input v-model="form.hjszd" placeholder="请输入户籍所在地/现住址" clearable />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="家属联系方式" prop="jslxfs">
                <el-input v-model="form.jslxfs" placeholder="请输入家属联系方式" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="死亡时间" prop="swsj">
                <el-date-picker
                  v-model="form.swsj"
                  type="date"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="请选择死亡时间"
                  style="width: 100%"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="死亡原因" prop="swyy">
                <el-input v-model="form.swyy" placeholder="请输入死亡原因" clearable />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSwmd } from "@/api/system/swmd";

export default {
  name: 'Add',
  data() {
    return {
      open: false,
      form: {
        xm: null,
        xb: null,
        sfzh: null,
        hjszd: null,
        jslxfs: null,
        swsj: null,
        swyy: null
      },
      rules: {
        xm: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        xb: [{ required: true, message: '性别不能为空', trigger: 'change' }],
        sfzh: [{ required: true, message: '证件号码不能为空', trigger: 'blur' }],
        hjszd: [{ required: true, message: '户籍所在地不能为空', trigger: 'blur' }],
        jslxfs: [{ required: true, message: '家属联系方式不能为空', trigger: 'blur' }],
        swsj: [{ required: true, message: '死亡时间不能为空', trigger: 'change' }],
        swyy: [{ required: true, message: '死亡原因不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleAdd() {
      this.reset()
      this.open = true
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        xm: null,
        xb: null,
        sfzh: null,
        hjszd: null,
        jslxfs: null,
        swsj: null,
        swyy: null
      }
      this.resetForm('form')
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addSwmd(this.form).then(response => {
            this.$modal.msgSuccess('公安数据新增成功')
            this.open = false
            this.$emit('success')
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.module-section {
  margin-bottom: 24px;
  padding: 16px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  background: #f8f9fa;
}

.module-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid #E6A23C;
}

.module-header i {
  font-size: 18px;
  margin-right: 8px;
  color: #E6A23C;
}

.module-title {
  font-size: 16px;
  font-weight: 600;
  color: #E6A23C;
  margin-right: 8px;
}

.module-remark {
  font-size: 12px;
  color: #f56c6c;
}
</style>
